package com.example.zzm.yuejian03.Adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zzm.yuejian03.Bean.MovieBean;
import com.example.zzm.yuejian03.R;
import com.example.zzm.yuejian03.Server;
import com.squareup.picasso.Picasso;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {

    private List<MovieBean> list;//向rv中填充的数据
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态布局

    //自定义 构造方法

    public MovieAdapter(Context context,List<MovieBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    //自定义 ViewHolder子类，容纳item视图
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        JCVideoPlayerStandard clubvideo;


        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            clubvideo =(JCVideoPlayerStandard) itemView.findViewById(R.id.club_video_name);
        }
    }

    //自定义 设置数据list
    public void setList(List<MovieBean> list) {
        this.list = list;
        notifyDataSetChanged();//通知RV刷新数据
    }

    @NonNull
    @Override  //为每一个item生成一个view
    public MovieAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie,parent,false);
        MovieAdapter.ViewHolder holder = new MovieAdapter.ViewHolder(view);
        return holder;
    }

    @Override  //为每个item填充数据，设置点击事件
    public void onBindViewHolder(MovieAdapter.ViewHolder holder, final int position) {
        final MovieBean bean = list.get(position);
        if (bean == null)
            return;
       // ViewHolder viewHolder = (ViewHolder) holder;
        holder.clubvideo.setUp(Server.IMAGEURL+bean.getName(), JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
        String path = "http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640";
        Uri uri = Uri.parse(path);
        holder.clubvideo.thumbImageView.setImageURI(uri);

        holder.textView.setText(bean.getIntro());
    }

    @Override  //确定item个数
    public int getItemCount() {
        if(list!=null)
            return list.size();
        else
            return 0;
    }
}
